<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
* Description of IndexController
*
*/

class Admin_PrevillegeController extends Zend_Controller_Action
{

	public function init(){
            ini_set('display_errors',false);
		 $this->previllegeupdatemess="";
	}

    public function indexAction()
    {
       Zend_Loader::loadFile("../application/admin/models/Previllege.php");
       $previllege=new Previllege();
         $this->_helper->layout->setLayout('admin_layout');
         $request = $this->getRequest();
          if($this->getRequest()->isPost())
          {
             // print_r($_POST);
              $previllege->saveprevilleges($_POST);
          }
       
       $controllerarr=$previllege->listControllers();
       $previllegearr=$previllege->getprevilleges();
       $rolearr=array("supervisor","consultant");
       $actionarr=array("index","add","edit","delete");
     //  print_r($previllegearr);
       $formattedarr=array();
       foreach($previllegearr as $parr)
       {
           $formattedarr[$parr["component"]][$parr["role"]][$parr["action"]]=$parr["type"];
           
       }
       //print_r($formattedarr);

       foreach ($controllerarr as $controller)
       {
           foreach($rolearr as $val){
                foreach($actionarr as $act)
                {
                      $prevviewarr[$controller["name"]][$val][$act]=$formattedarr[$controller["name"]][$val][$act];


                }


           }
           
       }
        $this->view->rolearr=array("supervisor","consultant");
        $this->view->actionarr=array("index","add","edit","delete");
       $stat = $request->getParam('stat');
       //  print_r($prevviewarr);//exit;
       $this->view->stat = $stat;
       $this->view->prevviewarr=$prevviewarr;
    }
       public function addAction()
    {
        Zend_Loader::loadFile('../application/admin/forms/previllege.php');
       $this->_helper->layout->setLayout('admin_layout');
        $controller = $this->getRequest()->getControllerName();
        $action = $this->getRequest()->getActionName();
        $options = array("action"=>$action);
        $previllege_form = new Form_previllegeform($options);
        $this->view->previllege_form = $previllege_form;
        $this->view->title = "Edit";

    }
       public function editAction()
    {
        Zend_Loader::loadFile("../application/admin/models/Previllege.php");
        Zend_Loader::loadFile("../application/admin/forms/previllege.php");
        $previllegemodel=new previllege();
        $this->_helper->layout->setLayout('admin_layout');
        $controller = $this->getRequest()->getControllerName();
        $action = $this->getRequest()->getActionName();
        $options = array("action"=>$action);
        $admin_id = $this->_getParam('admin_id', 0);
        $options = array("action"=>$action, "admin_id"=>$admin_id);
        $previllegeform = new Form_previllegeform($options);
        $this->view->previllege_form =  $previllegeform;
        $this->view->admin_id = $admin_id;
        if ($admin_id > 0) {
            $previllegeform->populate($previllegemodel->getprevillege($admin_id));
        }      
        $previllegedata=$previllegemodel->getprevillege($admin_id);
 

    }

     public function doaddAction()
    {
        Zend_Loader::loadFile("../application/admin/models/Previllege.php");
        Zend_Loader::loadFile("../application/admin/forms/previllege.php");
       print_r($_POST);
         $request = $this->getRequest();
        $modprevillege=new previllege();
        $controller = $this->getRequest()->getControllerName();
        $action = $this->getRequest()->getActionName();
        $options = array("action"=>$action);
        $previllegeform=new Form_previllegeform($options);
        if($this->getRequest()->isPost()){
            $formdata = $this->getRequest()->getPost();
            if($previllegeform->isValid($formdata)){
                    $insertarr["admin_firstname"]= preg_replace ( "/'/", "\\'", $request->getParam('admin_firstname') );
                    $insertarr["admin_lastname"]=preg_replace ( "/'/", "\\'", $request->getParam('admin_lastname') );
                    $insertarr["admin_email"]=preg_replace ( "/'/", "\\'", $request->getParam('admin_email') );
                    $insertarr["admin_previllegename"]=preg_replace ( "/'/", "\\'", $request->getParam('admin_previllegename') );
                    $insertarr["admin_password"]=preg_replace ( "/'/", "\\'", $request->getParam('admin_password') );
                    $insertarr["admin_previllegeType"]=preg_replace ( "/'/", "\\'",$request->getParam('admin_previllegeType') );
                    $insertarr["admin_status"]=preg_replace ( "/'/", "\\'", $request->getParam('admin_status') );
                    
                         $addprevillege=$modprevillege->addprevillege($insertarr)  ;
                    

                    if($addprevillege)
                    {
                         $this->_redirect('/admin/previllege/index/stat/updated');
                         $this->previllegeupdatemess="Successfully added!";

                    }
              }
              else{
                   $previllegeform->populate($formdata);
                   $this->_helper->layout->setLayout('admin_layout');
                   $this->view->previllege_form = $previllegeform;
              }
            
            }
            
    }

    public function doeditAction()
    {
        Zend_Loader::loadFile("../application/admin/models/Previllege.php");
        Zend_Loader::loadFile("../application/admin/forms/previllege.php");
      // print_r($_POST);
        $modprevillege=new previllege();
        
        $request = $this->getRequest();
        $controller = $this->getRequest()->getControllerName();
        $action = $this->getRequest()->getActionName();
        $options = array("action"=>$action);
        $previllegeform=new Form_previllegeform($options);
        
         if($this->getRequest()->isPost()){
            $formdata = $this->getRequest()->getPost();
            if($previllegeform->isValid($formdata)){
                    $insertarr["admin_id"]= preg_replace ( "/'/", "\\'", $request->getParam('admin_id') );
                    $insertarr["admin_firstname"]= preg_replace ( "/'/", "\\'", $request->getParam('admin_firstname') );
                    $insertarr["admin_lastname"]=preg_replace ( "/'/", "\\'", $request->getParam('admin_lastname') );
                    $insertarr["admin_email"]=preg_replace ( "/'/", "\\'", $request->getParam('admin_email') );
                    $insertarr["admin_previllegename"]=preg_replace ( "/'/", "\\'", $request->getParam('admin_previllegename') );
                    $insertarr["admin_password"]=preg_replace ( "/'/", "\\'", $request->getParam('admin_password') );
                    $insertarr["admin_previllegeType"]=preg_replace ( "/'/", "\\'",$request->getParam('admin_previllegeType') );
                    $insertarr["admin_status"]=preg_replace ( "/'/", "\\'", $request->getParam('admin_status') );
                $editprevillege=$modprevillege->editprevillege($insertarr)  ;
                if($editprevillege)
                {
                     //$this->_redirect('admin/previllege/edit/id/'.$insertarr["id"]);
                    $this->_redirect('admin/previllege/index/stat/updated');
                    $this->previllegeupdatemess="Successfully updated!";
                }
            }
            else{
                $previllegeform->populate($formdata);
                $this->_helper->layout->setLayout('admin_layout');
                $this->view->previllegeform = $previllegeform;
            }

             $this->_redirect('admin/previllege/index/stat/updated');
         }
        
    }

     public function deleteAction()
    {
        Zend_Loader::loadFile("../application/admin/models/Previllege.php");
      // print_r($_POST);
        $modprevillege=new previllege();
        $request = $this->getRequest();
        $admin_id=$request->getParam('admin_id');
        $deleteprevillege=$modprevillege->deleteprevillege($admin_id);
        if($deleteprevillege)
        {
             $this->_redirect('admin/previllege/index/stat/deleted');
        }
     }
     public function changestatusAction()
	{
		$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile("../application/admin/models/Previllege.php");
		$modprevillege = new previllege();
		$request = $this->getRequest();
		$admin_id = $request->getParam('admin_id');
		$modprevillege->changeprevillegeStatus($admin_id);
                exit;
	}

   public function getcontentAction()
	{
		$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile("../application/admin/models/Previllege.php");
		$modprevillege = new previllege();
		$request = $this->getRequest();
		$id = $request->getParam('id');
		echo $modprevillege->getcontent($id);
	}




}
?>
